package com.chen.controller;

import com.chen.bean.Blog;
import com.chen.bean.Page;
import com.chen.bean.Result;
import com.chen.service.BlogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/content")
public class BlogController {

    @Resource
    BlogService blogService;

    @PostMapping("/save")
    public Result saveContent(@RequestBody Blog blog){
        try {
            if(blog.getId()==null){
                if(blogService.loadContent(blog.getTitle())!=null){
                    return Result.error(1,"标题已存在",null);
                }
                Integer id = blogService.insertContent(blog);
                return Result.success(id);
            }else {
                blogService.updateById(blog);
                return Result.success(blog.getId());
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了",null);
        }
    }

    @GetMapping("/loadLabel")
    public Result loadLabelList(String username){
        try {
            List<String> labelList = blogService.getLabelList(username);
            return Result.success(labelList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(2,"出错了，请稍后重试",null);
        }
    }

    @GetMapping("/loadHotLabel")
    public Result loadHotLabelList(){
        try {
            List<String> labelList = blogService.loadHotLabel();
            return Result.success(labelList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了，请稍后重试",null);
        }
    }

    //注意加用户名
    @GetMapping("/loadTitle")
    public Result loadTitleList(String label,String username){
        try {
            List<String> titleList = blogService.getTitleList(label,username);
            return Result.success(titleList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(2,"出错了，请稍后重试",null);
        }
    }

    @GetMapping("/loadHotTitle")
    public Result loadHotTitleList(){
        try {
            List<String> titleList = blogService.loadHotTitle();
            return Result.success(titleList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(2,"出错了，请稍后重试",null);
        }
    }

    @GetMapping("/loadContent")
    public Result loadContent(String title){
        try {
            String content = blogService.loadContent(title);
            return Result.success(content);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了，请稍后重试",null);
        }
    }

    //修改文章内容时首先加载文章内容
    @GetMapping("/loadBlogOnUpd")
    public Result loadBlogOnUpd(String title){
        try {
            Blog blog = blogService.loadBlog(title);
            return Result.success(blog);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了，请稍后重试",null);
        }
    }

    @GetMapping("/loadBlogList")
    public Result loadBlogList(){
        try {
            List<Blog> blogList = blogService.loadBlogList();
            return Result.success(blogList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了，请重试",null);
        }
    }

    @GetMapping("/loadBlogListByLabel")
    public Result loadBlogListByLabel(@RequestParam("label") String label,
                                    @RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
                                    @RequestParam(name = "pageSize",defaultValue = "6") Integer pageSize){
        try {
            Page<List<Blog>> page = blogService.loadBlogListByLabel(label, pageNum, pageSize);
            return Result.success(page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了，请重试",null);
        }
    }

    @GetMapping("/judgeStar")
    public Result judgeExistStar(String title,String username){
        try {
            boolean star = blogService.existStar(title, username);
            return Result.success(star);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了，请重试",null);
        }
    }

    @GetMapping("/handleStar")
    public Result handleStar(String title,String username){
        try {
            boolean star = blogService.existStar(title, username);
            if (star){
                blogService.cancelStar(title,username);
            }else {
                blogService.starBlog(title,username);
            }
            return Result.success(null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了，请重试",null);
        }
    }

    @GetMapping("/loadStarBlog")
    public Result loadStarBlog(String username,Integer pageNum,Integer pageSize){
        try {
            Page<List<Blog>> page = blogService.loadStarBlog(username, pageNum, pageSize);
            return Result.success(page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了，请重试",null);
        }
    }
}
